/**
 * Created by WangMing on 15/12/9.
 */
define(["../../../service",
        "../../../utils/i18n",
        "highcharts",
        "../../../widgets/player_live_widget/player_live_widget",
        "../../../widgets/qrcodewidget/qrcode_widget"
    ],
    function(api, i18n, Highcharts) {
        // 定义所有相关的vmodel
        var vm = avalon.define({
            $id: "video",
            $video: {
                method: api.getVideoPlay
            },
            itemID: null,
            $error: {},

            $player: {},
            $videoResembleList: [],
            $skipArry: ["playerWidget", "itemID", "seek"],
            $setting: {},
            jwplayerSeek: function(second) {
                if (window.jw.seek&&second<=window.jw.getDuration()) {
                    window.jw.seek(second);
                } 
            }
        });

        

        i18n.ngettext(" has been reviews", "%d people has been reviews", 2);
        vm.$watch("setting", function(param) {
            vm.$setting = avalon.define(
                avalon.mix(vm.$setting, param))

        })

        return avalon.controller(function($ctrl) {

            // 视图渲染后，意思是avalon.scan完成
            $ctrl.$onRendered = function() {
                api.getItems({
                    method: api.setVideoPv,
                    query: {
                        id: vm.itemID
                    }
                }, function(res) {

                })
                var StudentRatio = parseFloat(vm.$video.Video.studentRatio);
                var TeacherRatio = parseFloat(vm.$video.Video.teacherRatio);

                return new Promise(function(res,rej){
                    res()
                }).then(function(){
                    return Highcharts.chart('info_chart', {
                        credits: {
                            enabled: false
                        },
                        chart: {
                            type: 'pie',
                            backgroundColor: "transparent",
                            //spacingTop: 10,
                            marginTop: 0,
                            marginBottom: 0,
                            marginLeft: 10
                        },
                        plotOptions: {
                            pie: {
                                slicedOffset: 2,
                                dataLabels: {
                                    enabled: true
                                }
                            }
                        },
                        title: {
                            margin: 0,
                            floating: true,
                            text: "教学行为活动比例",
                            style: { fontSize: '14px', lineHeight: '18px', color: '#ffffff' },

                            marginBottom: 0,
                            verticalAlign: "middle"
                        },
                        xAxis: {
                            title: "比例"
                        },
                        series: [{
                            data: [{
                                    name: "老师活动比例",
                                    y: TeacherRatio,
                                    sliced: true,
                                    selected: true
                                },
                                ['学生活动比例', StudentRatio]
                            ],
                            step: 'right',
                            name: '比例'
                        }]
                });
                Highcharts.chart()
                }).then(function(){
                    return Highcharts.chart('videost_chart',{
                        title: {
                            text: ''
                        },
                        credits: {
                            enabled: false
                        },
                        chart:{
                            zoomType:"xy",
                            backgroundColor:'#222222'
                        },
                        legend:{
                            enabled:false
                        },
                        plotOptions:{
                            line:{
                                marker:{
                                    radius:2
                                }
                            }
                        },
                        xAxis: {
                            min:0,
                            max: 60 * 60 * 1000,
                            tickInterval: 240 * 1000,
                            lineWidth: .5,
                            gridLineWidth: .5,
                            endOnTick: true,
                            tickWidth:0,
                            title: {
                                text: "学生行为时间",
                                align: "middle"
                            },
                            maxPadding: 0.1,
                            type: "datetime",
                            labels: {
                                 rotation: 0,
                                 x:-4,
                                
                                formatter: function () {
                                    //return Highcharts.dateFormat('%H:%M:%S', this.value);
                                    return this.value / 1000/6/10;
                                }
                            }
                            // categories:[0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]
                        },
                        yAxis: {
                            min: 0,
                            max: 60 * 60 * 1000,
                            tickInterval: 240 * 1000,
                            lineWidth: .5,
                            gridLineWidth: .5,
                            type: "datetime",
                            maxPadding: 0.1,
                            endOnTick :true,
                            title: {
                                text: "教师行为时间",
                                align: "middle",

                            },
                            labels: {
                                formatter: function () {
                                    //return Highcharts.dateFormat('%H:%M:%S', this.value);
                                    return this.value / 1000/6/10;
                                }
                            }
                            // categories:[0,4,8,12,16,20,24,28,32,36,40,44,48,52,56,60]

                        },
                       
                        tooltip: {
                            enable:false,
                            formatter: function () {
                       //         return '';
                                return '' +
                                        '教师行为时间' + Highcharts.dateFormat('%H:%M:%S ', this.y) +
                                        '学生行为时间' + Highcharts.dateFormat('%H:%M:%S ', this.x);
                           }
                        },
                        series: [{
                            data:  vm.$video.videoST.data,
                            step: 'right',
                            type:'line',
                            pointInterval: 30 * 1000,
                            name: '行为曲线'
                        }]

                    });
                })

            };
            // 进入视图
            $ctrl.$onEnter = function(param, rs, rj) {
                console.log('test');
                vm.$video = {
                    method: api.getVideoPlay
                },
                vm.$video.seekTime = param.seekTime;
  
                // if(String(param.itemID).indexOf("#") != -1){
                //     avalon.router.redirect(String(param.itemID).replace(/[^\d].*$/, ''))
                //     return;
                // }
                vm.itemID = param.itemID
                vm.$video.videoST = {};
                return new Promise(function(res, rej) {
                    res();
                }).then(function() {

                    return api.getItems({
                        method: api.getVideo,
                        query: {
                            id: vm.itemID
                        }
                    }, function(res) {
                        if (res.success) {
                            console.log(res)
                            vm.$video.Video = {};
                            vm.$video.Video = _.merge(vm.$video.Video, res.data);
                            vm.$video.Video.size = (vm.$video.Video&&vm.$video.Video.length)||0
                         } else {
                            vm.$video.Video = null
                            vm.$error = res;
                        }

 
                    })
                }).then(function() {
                    return api.getItems({
                        method: api.getVideoResemble,
                        query: {
                            id: vm.itemID,
                            PageSize: 8
                        }
                    }, function(res) {
                        vm.$videoResembleList = {}
                        if (res.success) {
                            vm.$videoResembleList = {
                                results: {
                                    collections: res.list||[]
                                }, 
                                size: (res.list && res.list.length) || 0,
                                isAjax: false
                            };
                        } else {
                            vm.$error = res;
                            vm.$videoResembleList = null;
                        }
                    });

                }).then(function() {
                    return api.getItems({
                        method: api.getVideoTimeInfo,
                        query: {
                            id: vm.itemID

                        }
                    }, function(res) {
                        vm.$video.VideoInfo = [];
                        if (!res.success) {
                            vm.$video.VideoInfo.error = true;
                        } else {
                            vm.$video.VideoInfo = _.merge(vm.$video.VideoInfo, res.data);
                            vm.$video.VideoInfo.size = vm.$video.VideoInfo.length
                        }
                    })
                }).then(function() {
                    return api.getItems({
                        method: api.getVideoSW,
                        query: {
                            id: vm.itemID

                        }
                    }, function(res) {
                        vm.$video.VideoSW = [];
                        if (!res.success) {
                            vm.$video.VideoSW.error = true;
                        } else {
                            vm.$video.VideoSW = _.merge(vm.$video.VideoSW, res.data);
                            vm.$video.VideoSW.size = vm.$video.VideoSW.length
                        }
                    })
                }).then(function(){
                    // 行为分析
                    return api.getItems({
                        method:api.getVideoST,
                        query:{
                            id:vm.itemID
                        }
                    },function(res){
                        console.log(res)
                        vm.$video.videoST.data = [];
                        if(!res.success){
                            vm.$video.videoST.error = true;
                        }else{
                            vm.$video.videoST = _.merge(vm.$video.videoST,res.data)
                            var s = 0,
                                t = 0;
                            for(var i=0;i<res.data.length;i++){
                                if(res.data[i].tag==1){
                                    s = res.data[i].seconds;
                                }else if(res.data[i].tag==0){
                                    t = res.data[i].seconds;
                                }
                                vm.$video.videoST.data.push([s*1000,t*1000])
                            }
                        }
                            vm.$video.videoST.size = vm.$video.videoST.data.length || 0
                       

                    })
                }).then(function() {
                    console.log('**************************************')

                    return api.getItems({
                        method: api.getVideoPlay,
                        query: {
                            id: vm.$setting.vodVideoId || vm.itemID
                        }

                    }, function(res) {





                        vm.$video.VideoResourcePath = avalon.define(
                            avalon.mix(vm.$video.VideoResourcePath,
                                avalon.mix({ $id: "VideoResourcePath" }, res)));
                         vm.$video.smil = "/json/videoplay.smil?id="+(vm.$setting.vodVideoId || vm.itemID)+"&smil=1";
                         vm.$video.timeinfo = "/json/videoTimeInfo.action?id="+(vm.$setting.vodVideoId || vm.itemID)+"&rss=1";
                         vm.$video.videoSW = "/json/videoSW.action?id="+(vm.$setting.vodVideoId || vm.itemID)+"&rss=1";
                         
                    })  
                })

            };
            // 对应的视图销毁前
            $ctrl.$onBeforeUnload = function() {};
            $ctrl.$vmodels = [vm];
        })
    });
